package com.example.permissionsapply;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class Main2Activity extends AppCompatActivity {

    public static final int request_Permissions_Request_Code = 88;
    private static final String TAG = "Main2Activity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        findViewById(R.id.btn2_call_phone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过该方法检测某个权限是否被用户同意
                if (ActivityCompat.checkSelfPermission(Main2Activity.this, Manifest.permission.CALL_PHONE)
                        != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Main2Activity.this,
                        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    applyPermission();
                    return ;
                }

            }
        });


        findViewById(R.id.btn_test_apply).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Main2Activity.this,MainActivity.class));
            }
        });
    }

    /**
     * 申请权限
     */
    private void applyPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            /**
             *  @see #requestPermissions(String[], int)
             *  第一个参数是需要申请的权限，需要请求几个权限就在数组里面添加几个
             *  第二个参数是请求回调的标示码,通过onRequestPermissionsResult（int requestCode, @NonNull
             *      String[] permissions, @NonNull int[] grantResults）的requestCode参数返回
             */
            requestPermissions(new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.ACCESS_FINE_LOCATION},request_Permissions_Request_Code);
        }
    }

    /**
     * 当申请权限的时候回调该方法。（给用户弹出对话框提示用户是否通过权限，当用户点击拒绝或同意的时候就回调该方法。
     *                              如果用户已经 同意/拒绝 该权限则不会弹出对话框，这个时候会直接回调该方法。）
     * @param requestCode  通过 requestPermissions(String[], int)方法请求权限的时候传入的requestCode
     * @param permissions  请求的权限
     * @param grantResults  请求的权限所对应的返回结果，同意=0 拒绝=-1 参考{@link android.content.pm.PackageManager#PERMISSION_GRANTED}
     *                       和 {@link android.content.pm.PackageManager#PERMISSION_DENIED}.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        Log.i(TAG,"requestCode = " +requestCode);
        for (int i = 0; i < permissions.length; i++) {
            Log.i(TAG,"权限:" + permissions[i] + " ,grantResults = " +grantResults[i]);
        }

    }
}
